package com.nj9you.sdk.formatter;

import com.nj9you.sdk.http.Response;
import com.nj9you.sdk.params.User;

import org.json.JSONObject;

/**
 * Created by ck01- on 2017/7/4.
 */

public class UserJsonFormatter extends Formatter<User> {

    @Override
    public Response<User> format(String content) {
        Response<User> response = new Response<User>();
        User user = new User();
        response.setObj(user);
        try {
            JSONObject jobj = new JSONObject(content);
            if (jobj.has("state")) {
                response.setCode(jobj.getString("state"));
            }
            if (jobj.has("desc")) {
                response.setMsg(jobj.getString("desc"));
            }
            if (jobj.has("user")) {
                JSONObject userObj = jobj.getJSONObject("user");
                if (userObj != null) {
                    if (userObj.has("username")) {
                        user.setUsername(userObj.getString("username"));
                    }
                    if (userObj.has("gendar")) {
                        user.setUsername(userObj.getString("gendar"));
                    }
                    if (userObj.has("userKey")) {
                        user.setUsername(userObj.getString("userKey"));
                    }
                    if (userObj.has("id")) {
                        user.setUsername(userObj.getString("id"));
                    }
                    if (userObj.has("nickName")) {
                        user.setUsername(userObj.getString("nickName"));
                    }
                    if (userObj.has("birthday")) {
                        user.setUsername(userObj.getString("birthday"));
                    }
                    if (userObj.has("age")) {
                        user.setUsername(userObj.getString("age"));
                    }
                }
            }
        } catch (Exception e) {
            return null;
        }
        return response;
    }
}
